#include <iostream>

/*飞行行为，用抽象类表示*/
class FlyBehavior
{
public:
   virtual ~FlyBehavior(){};
   virtual void  fly() =0;
};

/*叫声行为，用抽象类表示*/
class QuackBehavior
{
public:
   virtual ~QuackBehavior(){};
   virtual void  quack()= 0;
};

/*鸭子基类，有 display, performFly，performQuack，swim等方法。*/
class Duck
{
public:
   Duck(FlyBehavior * p_FlyBehavior,QuackBehavior * p_QuackBehavior)
   {
      pFlyBehavior  = p_FlyBehavior;
      pQuackBehavior= p_QuackBehavior;

   }
   virtual ~Duck(){};
   virtual void display(){};
   void performFly()  // 执行飞
   {
      pFlyBehavior->fly();
   }

   void performQuack() // 发出嘎嘎叫
   {
      pQuackBehavior->quack();
   }

   void swim() // 游泳
   {
      std::cout<<("All ducks float, even decoys!")<< std::endl;
   }
private:
   FlyBehavior   * pFlyBehavior;    // 飞的行为
   QuackBehavior * pQuackBehavior;
};

/*实现飞行行为的方法类*/
class FlyWithWings : public FlyBehavior
{
	public:
   void fly()
   {
	   std::cout<< ("I'm flying!!")<<std::endl;
   }
};

class FlyNoWay : public FlyBehavior  // 不会飞
{
public:
   void fly()
   {
	   std::cout<< ("I can't fly")<<std::endl;
   }
};

/*实现叫声行为的类*/
class Quack : public QuackBehavior
{
public:
   void quack()
   {
	   std::cout<< ("Quack") <<std::endl;
   }
};

class MuteQuack : public QuackBehavior // 哑巴
{
public:
   void quack()
   {
	   std::cout<< ("<< Slience >>")<< std::endl;
   }
};

class Squeak : public QuackBehavior
{
public:
   void quack()
   {
	   std::cout<< "Squeak"<<std::endl;
   }
};
/*绿头鸭类*/
class MallardDuck : public Duck
{
public:
   MallardDuck(FlyBehavior * fly_behavior = new FlyWithWings(),
      QuackBehavior * quack_behavior = new Quack())
      :Duck(fly_behavior,quack_behavior){}

   void display()
   {
      std::cout << std::endl;
      std::cout<< "I'm a real Mallard duck"<< std::endl;
   }
};

int main()
{
   Duck * mallard = new MallardDuck();
   mallard->display();
   mallard->performFly();
   mallard->performQuack();
   return 0;
}
